package com.teradata.fbj.easy;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.teradata.fbj.pojo.linkParams;
import com.teradata.fbj.util.GetConnection;
import com.teradata.fbj.util.freeJDBC;

@Controller
@RequestMapping(value="/")
public class EasyUIStudy {
	
	@Autowired
	private GetConnection getcon = new GetConnection();
	private freeJDBC freeJ = new freeJDBC();
	private Connection connection = null;
	private PreparedStatement ps = null;
	private ResultSet rs = null;
	
		@RequestMapping(value = "/getUsers.action",method=RequestMethod.POST)
		public @ResponseBody List<Easybean> getUsers(){
			List<Easybean> list = new ArrayList<Easybean>();
			
			linkParams lp = new linkParams();
			lp.setDriverClass("com.mysql.jdbc.Driver");
			lp.setBaseUrl("jdbc:mysql://localhost:3306/how2java");
			lp.setUsername("root");
			lp.setPassword("fubjie");
			
			try {
				connection = getcon.getConnection(lp);
				if(connection ==null){
					System.out.println("连接失败！");
				}
				String sql = "select * from users";
				ps= connection.prepareStatement(sql);
				rs = ps.executeQuery();
				while(rs.next()){
					Easybean eb = new Easybean();
					eb.setFirstname(rs.getString("firstname"));
					eb.setLastname(rs.getString("lastname"));
					eb.setPhone(rs.getString("phone"));
					eb.setEmail(rs.getString("email"));
					list.add(eb);
				}
			} catch (Exception e) {
				// TODO: handle exception
			}finally {
				freeJ.free(rs, ps, connection);
			}
			return list;
		}
		
}
